<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the MX_Loader class */

class MY_Loader extends CI_Loader {
    
    function widget($widget = '', $params = NULL, $object_name = NULL) {
        if ($widget == '') {
            return FALSE;
        }

        if (!is_null($params) AND !is_array($params)) {
            $params = NULL;
        }

        if (is_array($widget)) {
            foreach ($widget as $class) {
                $this->_ci_load_widget($class, $params, $object_name);
            }
        } else {
            $this->_ci_load_widget($widget, $params, $object_name);
        }
    }

    function _ci_load_widget($class, $params = NULL, $object_name = NULL) {
        $class .= '/' . $class;
        $subdir = '';
        if (strpos($class, '/') !== FALSE) {
            // explode the path so we can separate the filename from the path
            $x = explode('/', $class);

            // Reset the $class variable now that we know the actual filename
            $class = end($x);

            // Kill the filename from the array
            unset($x[count($x) - 1]);

            // Glue the path back together, sans filename
            $subdir = implode($x, '/') . '/';
        }

        foreach (array($class) as $class) {
            $widget_class = APPPATH . 'widgets/' . $subdir . $class . EXT;
            // Safety:  Was the class already loaded by a previous call?
            if (in_array($widget_class, $this->_ci_loaded_files)) {
                // Before we deem this to be a duplicate request, let's see
                // if a custom object name is being supplied.  If so, we'll
                // return a new instance of the object
                if (!is_null($object_name)) {
                    $CI = & get_instance();
                    if (!isset($CI->$object_name)) {
                        return $this->_ci_init_class($class, '', $params, $object_name);
                    }
                }

                $is_duplicate = TRUE;
                log_message('debug', $class . " widget already loaded. Second attempt ignored.");
                return;
            }

            include_once($widget_class);
            $this->_ci_loaded_files[] = $widget_class;
            return $this->_ci_init_class($class, '', $params, $object_name);
        }
    }
}